package cn.itcast.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService{
	//注入Dao
	@Autowired
	private CustomerRepository customerRepository;

	@Override
	//查询所有未关联定区的客户
	public List<Customer> findNoAssociationCustomers() {
		return customerRepository.findByFixedAreaIdIsNull();
	}

	@Override
	//查询已经关联定区的客户
	public List<Customer> findHasAssociationFixedAreaCustomers(String fixedAreaId) {
		
		return customerRepository.findByFixedAreaId(fixedAreaId);
	}

	@Override
	//将客户关联到定区  客户id拼接
	public void associationCustomersToFixedArea(String customerIdStr, String fixedAreaId) {
		// 首先对字符串进行切割
		String[] customerIdArray = customerIdStr.split(",");
		for (String customerId : customerIdArray) {
			Integer id = Integer.parseInt(customerId);
			customerRepository.updateFixedAreaId(fixedAreaId,id);
		}
		
		
		
	}
     //保存用户
	@Override
	public void regist(Customer customer) {
		customerRepository.save(customer);
		
	}
     //通过电话号码查找用户
	@Override
	public Customer findCustomerByTelephone(String telephone) {
		
		return customerRepository.findByTelephone(telephone);
	}

	@Override
	public void updateType(String telephone) {
		customerRepository.updateType(telephone);
		
	}

}
